package radio.vnmedia.vn.utils.htmlparser;

import java.util.ArrayList;

public abstract class FindToFocus {
	private ArrayList<String> tag_open;
	private ArrayList<String> tag_close;

	// private boolean return_all_focus_level = false;
	/**
	 * This is the class using to find a string by the method find outsite to inside.\n
	 * By the way use the tags open and close. Your string need to find will
	 * be\n found in inner tag.\n For example: I have a text:\n
	 * "abc|123a;bcgd.df.|gf;gd.fg.fdfsdf"\n open_tag = {"|", ";"}\n close_tag
	 * ={".", "."}\n
	 * 
	 * String output is: bcgd gd\n
	 * 
	 * This find method will be necessary for you when you want to find a text\n
	 * in a html/xml page. You will easy to bookmark a string in a page html\n
	 * source code that no need worry content of that page modify.\n
	 * 
	 * @param tag_open
	 *            Every element in tag open must the same with a tag in
	 *            FF_close_chude. The first element is parent tag of the second
	 *            element, and...
	 * @param FF_close_chude
	 * @throws Exception
	 */
	public FindToFocus(ArrayList<String> tag_open, ArrayList<String> tag_close) throws Exception {
		this.tag_open = tag_open;
		this.tag_close = tag_close;
		// this.return_all_focus_level = return_all_focus_level;
		if (tag_close == null || tag_open == null || tag_close.size() != tag_open.size()) {
			throw new Exception("tag open & tag close is not valid. length of tag open must equal with tag close and they is not null");
		}
	}

	public abstract void stringFocus(String str);

	public void findFocus(String content, int tag_index) {
		if (tag_index < tag_open.size()) {
			// System.out.println("findFocus: tag_index=" + tag_index +
			// ", tag_open[tag_index]=" + tag_open.get(tag_index));
			BlockTextFinder btf = new BlockTextFinder(content, 0, tag_open.get(tag_index), tag_close.get(tag_index));
			// has parrent tag
			while (btf.hasNext()) {
				String sub_content = btf.getText();
				// Parent to child
				if (tag_index == tag_open.size() - 1) {
					// process content

					stringFocus(sub_content);
				}
				findFocus(sub_content, ++tag_index);
				tag_index--;
			}
		}
	}
}